package mid

import (
	"fmt"
	"strconv"
	"strings"
	"unicode"
)

func DiscountPrices(sentence string, discount int) string {
	words := strings.Split(sentence, " ")
	div := float64(discount) / 100.0
	for i := 0; i < len(words); i++ {
		str := words[i]
		if str[0] == '$' && isNumeric(str[1:]) {
			temp := str[1:]
			price, err := strconv.ParseFloat(temp, 64)
			if err != nil {
				continue
			}
			discountedPrice := price - price*div
			words[i] = fmt.Sprintf("$%.2f", discountedPrice)
		} else {
			words[i] = str
		}
	}
	return strings.Join(words, " ")
}

func isNumeric(s string) bool {
	if len(s) == 0 {
		return false
	}
	for _, c := range s {
		if !unicode.IsDigit(c) {
			return false
		}
	}
	return true
}
